<!DOCTYPE html>
<html>
<head>
    <title>项目信息</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="../../common/js/public.js" ></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground left" style="float: left;width:400px">
        <div class="titlediv">
            <p style="text-align:right;background-color:white;line-height: 30px">
                公司信息<i class="layui-icon layui-icon-refresh" style="margin-left: 36%;cursor: pointer;" onclick="reshtreeroot()"></i>&ensp;&ensp;
            </p>

        </div>
        <div class="searchdiv" >
            <div class="layui-input-inline" style="width:100%">
                <input type="text" id="search" name="keyword" autocomplete="off" class="layui-input" placeholder="请输入名称" style="height: 30px; !important;"/>
                <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="searchByName()"></i>
            </div>
        </div>
        <div class="ztreediv" id="ztreediv">
            <div class="ztreeuldiv">
                <ul id="treeStructure" class="ztree" style="height: 400px;"></ul>
            </div>
        </div>
    </div>
</div>
<script>
    var nodeSelected,node;
    $(document).ready(function() {
        node = window.parent.nodeSelected;
        initTree("#treeStructure");
    })

    /* *
     * @Author bxf
     * @Description setting配置
     * @Date 14:33 2019/8/28
     * @param null
     * @return
     */
    var setting = {
        check: {
            //enable: true,
            //chkboxType: { "Y": "s", "N": "s" }
        },
        edit:{
            enable: false,
        },
        async: {
            enable: true,
            url: publicUrl+"company/getByPid",
            headers: {"Authorization":getData("Authorization")},
            dataFilter: formatedata,
            otherParam:{//传入查询参数
                //注意，要传入动态变化的参数必须用return的方式
                "name":function(){return $("#search").val()
                }
            },

            autoParam: ["id"]
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"id",
                pIdKey:"pid",
                isParent:"parent",
                rootPid:""
            },
            key: {
                isParent:"parent",
                url: "xUrl",
                icon:"xIcon",
                level:"orgLevel",
                name:"shortName"
            }
        },
        callback: {
            beforeClick: beforeClick,
            onClick: zTreeOnClick
        }
    };

    window.onload = getContentSize;
    window.onresize = getContentSize;
    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
    }
    /* *
     * @Author bxf
     * @Description 刷新树
     * @Date 11:16 2019/8/28
     * @param null
     * @return
     */
    function refreshRoot(){
        initTree("#treeStructure");
    }
    //zTreeObj = $.fn.zTree.init($("#treeStructure"), setting, zNodes);
    function formatedata(treeId,parentNode,responseData) {
        return changeIcon(formatTreeParseData(treeId,parentNode,responseData,null));
    }

    function searchByName(){
        var search = $("#search").val();
        if(!search){
            return ;
        }
        loadTree("#treeStructure");
    }
    /* *
     * @Author bxf
     * @Description 加载树
     * @Date 14:31 2019/8/28
     * @param null
     * @return
     */
    function loadTree(id){
        $(id).html('');
        publicAjax (null,'orgstation/getProjectsForTree',{"name":$("#search").val(),"stationFlag":2},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
            zTreeObj.expandAll(true);
        })
    }

    function initTree(id){
        $(id).html('');
        publicAjax(null,'company/getByPersonId',{},false,null,function(data){
            zTreeObj = $.fn.zTree.init($(id),setting, changeIcon(data.data));
        })
    }

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    function beforeClick(treeId, treeNode, clickFlag) {
        if(treeNode.hierarchy >= node.hierarchy){
            layer.msg("组织级别不合法！",{time:500});
            return false;
        }
    }


    function zTreeOnClick(event, treeId, treeNode, clickFlag) {
        console.log(treeNode.level);
        nodeSelected = treeNode;

    }
</script>

</body>
</html>